package com.first.agent.service.impl;

import com.first.agent.service.UserService;
import com.first.agent.common.R;
import com.first.agent.dao.UserDao;
import com.first.agent.dao.impl.UserDaoImpl;
import com.first.agent.pojo.User;

import java.sql.SQLException;

/*
登录功能
 */
public class UserServiceImpl implements UserService {
    //创建dao对象
    private UserDao userDao = new UserDaoImpl();
    @Override
    public R login(String username, String password) {
        R r = new R();
        //调用dao
        try {
            User user = userDao.findUserByUsernameAndPwd(username, password);
            if(user!=null){
                //登录成功
                r.setCode(200);
                r.setMsg("登陆成功");
                r.setData(user);
            }else{
                r.setCode(500);
                r.setMsg("用户名或者密码错误");
            }
        } catch (SQLException e) {//出现异常?进入catch中
            e.printStackTrace();
            r.setCode(501);
            r.setMsg("系统开小差，请联系管理员");
        }

        return r;
    }
}
